/*	Interactive Gravitational Simulator
	Project: IGS Core
	File: Node.h
	Author: Mike Bantegui <mbante2@gmail.com>, Hofstra University 
	Copyright (C) 2012 - 2013 Mike Bantegui

	This file is part of the IGS software package for simulating
	the N-Body problem in real-time. This was developed as part
	of a departmental honors thesis project at Hofstra University
	in the Spring 2012 semester.

	IGS is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	IGS is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#ifndef IGS_NODE_H
#define IGS_NODE_H

#include "Graviton.h"
#include "Tensor.h"
#include <vector>

namespace IGS
{

// Represents a composite gravitational object that participates within
// the hierachical force evaluation method. Nodes act as sources of
// gravity in the Direct force calculation algorithm. In the Fast
// Multipole Method, Nodes act as both sources and sinks of gravity.
struct Node : public Graviton
{
	// The constituent parts of this Node
	std::vector<Graviton *> Branches;
#ifndef PoolAllocator
	// Whether the Node is a leaf
	bool IsLeaf;
#endif
	// The squared critical radius used for the acceptance criteria
	double CriticalRadiusSq;
#if MultipolePotentialTerms >= 1
	// The quadrupole term in the potential evaluation
	Tensor<2> Quadrupole;
#endif
#if MultipolePotentialTerms >= 2
	// The octupole term in the potential evaluation
	Tensor<3> Octupole;
#endif
#if MultipolePotentialTerms >= 3
	// The hexadecapole term in the potential evaluation
	Tensor<4> Hexadecapole;
#endif

	Node();
	Node(const std::vector<Node *> &branches, const double thetaSq);
	Node(const std::vector<Body *> &bodies, const double thetaSq);
	~Node();

	ViewNode *AsReadOnly() const;

	void CheckTimescale(const double timescaleSquared);

	void Pairwise(Body &other, const double softening);
	void Direct(Body &other, const double softening) const;

	double MultipolePotential(const Vector &dr, const double r1, const double r2) const;
	void AddMultipole(Node &branch) const;

private:
	void CalculateMultipole();
	void CalculateSize(const double thetaSq);
};

}

#endif